#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2011
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author: Jesus Carrero <j.o.carrero@gmail.com>
#

from scipy import empty, c_, linspace, dot, zeros


class FemToReal:

    """
    Simple post processing routine to values a function
    represented as a vector in a 1 dimesional, lagrange P1,
    finite element space ( hat functions )

    grid: sequence of increasing numbers.
    fem_solu: vector of real numbers
  """

    __solt__ = ['grid', 'basis']

    def __init__(self, grid, basis):
        """ instantaite the object. """

        self.grid, self.m_shape_functs = grid, basis

        self.deriv_at_quads, self.m_seg, self.sderAtQuadPts = None, None, None
        self.m_lcoor, self.m_shape_at_quads = None, None

        self._init_engine()

    def _init_engine(self):
        """ precompute all what is needed in order to recontruct solutions. """

        xi = self.grid.get_grid()
        self.m_seg = c_[xi[0:-1], xi[1:]]

        lcoor = linspace(-1., 1., 10, 'False')

        self.m_shape_at_quads = self.m_shape_functs.values(lcoor)
        self.deriv_at_quads = self.m_shape_functs.derivatives(lcoor)
        self.sderAtQuadPts = self.m_shape_functs.second_der(lcoor)

        self.m_lcoor = (lcoor + 1. ) / 2.

    def reconstruct(self, fv):
        """
      basis : is a basis of fem space
      grid: structure
      fv  : vector of dimension dof*nSeg
    """

        seg = self.m_seg
        dof = self.m_shape_functs.dof()
        nSeg = self.m_seg[:, 0].size
        lcoor = self.m_lcoor

        shape_at_quads = self.m_shape_at_quads
        deriv_at_quads = self.deriv_at_quads
        sderAtQuadPts = self.sderAtQuadPts

        supp = self.m_shape_functs.shape_func_support()
        width = supp[1] - supp[0]
        reco_solu = zeros((10 * nSeg, 4), 'float')
        for (i, se) in enumerate(seg):
            fAtSeg = fv[i * (dof - 1):i * (dof - 1) + dof]
            l = se[1] - se[0]
            reco_solu[i * 10:(i + 1) * 10, 0] = se[0] + l * lcoor

            reco_solu[i * 10:(i + 1) * 10, 1] = dot(fAtSeg.T, shape_at_quads)
            reco_solu[i * 10:(i + 1) * 10, 2] = dot(fAtSeg.T, deriv_at_quads) \
                / (l / width)
            reco_solu[i * 10:(i + 1) * 10, 3] = dot(fAtSeg.T, sderAtQuadPts) \
                / (l / width) ** 2

        return reco_solu


